package controls.visu;

import gui.visu.components.Between;

import java.awt.Color;

import data.visu.DisplayDataList;
import data.visu.datasets.BetweenDataSet;
import data.visu.enums.E_BetweenTypes;
import data.visu.enums.E_DisplayDataTypes;
import data.visu.structures.PairOfDouble;
import data.visu.structures.Range;
import data.visu.structures.RangeAndValue;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class BetweenControl extends A_DisplayDataControl{



	private E_BetweenTypes betweenType;

	public BetweenControl(String name, Color color, DisplayDataList displayDataList, E_BetweenTypes type){
		super(name, displayDataList, E_DisplayDataTypes.BETWEEN, color);

		setComparable(type.isComparable());
		setModifiable(type.isModifiable());

		setType(E_DisplayDataTypes.BETWEEN);
		setBetweenType(type);
		setDataSet(new BetweenDataSet());

		setyMinView(0);
		setyMaxView(type.getyMax());
	}
	
	@Override
	public void convertRangeAndValueOnRef(RangeAndValue rv) {
		double amplitudeX = getxMaxView() - getxMinView();
		double amplitudeY = getyMaxView() - getyMinView();
		double ratioX = (a_DisplayData.getBounds().getWidth()-a_DisplayData.getBounds().getWidth()/3) / amplitudeX;
		double ratioY = (a_DisplayData.getBounds().getHeight()) / amplitudeY;
		int newXMin = (int) ((rv.getRange().getMin() - getxMinView()) * ratioX);
		int newXMax = (int) ((rv.getRange().getMax() - getxMinView()) * ratioX);
		double newValueMin =  ((((PairOfDouble)(rv.getValue())).getMin() - getyMinView()) * ratioY);
		double newValueMax =  ((((PairOfDouble)(rv.getValue())).getMax() - getyMinView()) * ratioY);
		newValueMin = (int) (a_DisplayData.getBounds().getHeight() - newValueMin);
		newValueMax = (int) (a_DisplayData.getBounds().getHeight() - newValueMax);

		rv.setRange(new Range(newXMin, newXMax, rv.getRange().getDisplayDataControl()));
		rv.setValue(new PairOfDouble(newValueMin, newValueMax));
	}

	@Override
	protected void load_SetSubType(int typeIndex){
		E_BetweenTypes[] betweenTypes = E_BetweenTypes.values();
		setBetweenType(betweenTypes[typeIndex]);
	}
	
	@Override
	protected int save_GetSubType(){
		return betweenType.ordinal();
	}

	//Getters & Setters


	public void setBetween(Between between) {
		this.a_DisplayData = between;
	}

	public Between getBetween() {
		return (Between) a_DisplayData;
	}

	public void setBetweenType(E_BetweenTypes betweenType) {
		this.betweenType = betweenType;
	}

	public E_BetweenTypes getBetweenType() {
		return betweenType;
	}

}
